import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/widget/w_line.dart';
import 'package:yuan_hang_app/widget/w_loading.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

class Toast {
  static toast(text,{duration}) {
    dismissLoading();
    BotToast.showText(
        text: text,
        duration: duration??const Duration(seconds: 1),
        contentColor: const Color.fromRGBO(51, 51, 51, 0.8),
        textStyle:
            TextStyle(fontSize: 14.px, color: Colors.white, height: 10 / 7),
        borderRadius: BorderRadius.circular(2.px),
        contentPadding: EdgeInsets.symmetric(vertical: 8.px, horizontal: 16.px),
        align: Alignment.center);
  }

  static showLoading(
      {text = "努力加载中...", clickClose = false, Widget? widget, align}) {
    dismissLoading();
    BotToast.showCustomLoading(
        clickClose: clickClose,
        animationDuration: const Duration(milliseconds: 150),
        animationReverseDuration: const Duration(milliseconds: 150),
        backgroundColor: Colors.transparent,
        align: align ?? Alignment.center,
        toastBuilder: (_) {
          return widget ??
              Container(
                  padding: EdgeInsets.all(12.px),
                  decoration: BoxDecoration(
                      color: const Color.fromRGBO(51, 51, 51, 0.8),
                      borderRadius: BorderRadius.all(Radius.circular(4.px))),
                  child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        const WLoading(),
                        text == null
                            ? SizedBox(width: 5.px)
                            : Container(
                                padding: EdgeInsets.only(top: 4.px),
                                child: Text(text,
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 14.px)))
                      ]));
        });
  }

  static dismissLoading() {
    BotToast.closeAllLoading();

  }

  static showAlertDialog(
      {content = "...", conformText  = "确认",clickClose = false, Function? onConform}) {
    BotToast.showCustomLoading(
        clickClose: clickClose,
        animationDuration: const Duration(milliseconds: 150),
        animationReverseDuration: const Duration(milliseconds: 150),
        backgroundColor: WColors.black80,
        align: Alignment.center,
        toastBuilder: (_) {
          return Container(
            width: SizeUtils.screenW() * 0.65,
            decoration: BoxDecoration(
                color: WColors.white,
                borderRadius: BorderRadius.circular(10.px)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(height: 16.px),
                WText(
                  "提示",
                  style: TextStyle(
                    color: WColors.mainTextColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 16.px,
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(
                      left: 15.px, right: 15.px, bottom: 18.px, top: 10.px),
                  alignment: Alignment.center,
                  child: WText(
                    content ?? "",
                    style: TextStyle(
                      color: WColors.mainTextColor,
                      fontSize: 14.px,
                    ),
                  ),
                ),
                WLine(
                  height: 1.px,
                ),
                GestureDetector(
                  onTap: () {
                    dismissLoading();
                    if (onConform != null) {
                      onConform();
                    }
                  },
                  child: Container(
                    alignment: Alignment.center,
                    height: 44.px,
                    decoration: BoxDecoration(
                        color: WColors.white,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(10.px),
                            bottomRight: Radius.circular(10.px))),
                    child: WText(
                      conformText,
                      softWrap: false,
                      style: TextStyle(
                        color: WColors.primaryColor,
                        fontSize: 14.px,
                      ),
                    ),
                  ),
                )
              ],
            ),
          );
        });
  }
}
